package com.viethp.mt.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.viethp.mt.activities.R;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.objects.OItem;

public class OItemAdapter extends ArrayAdapter<OItem> {

	private Context context;
	private ArrayList<OItem> oitems;

	public OItemAdapter(Context context, int textViewResourceId,
			ArrayList<OItem> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.oitems = objects;
	}

	public static class ViewHolder {
		protected TextView tv_title;
		protected TextView tv_exp;
		protected TextView tv_price;
		protected TextView tv_username;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		ViewHolder viewholder;
		if (v == null) {
			viewholder = new ViewHolder();

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.lv_items, null);
			viewholder.tv_title = (TextView) v.findViewById(R.id.tv_title);
			viewholder.tv_exp = (TextView) v.findViewById(R.id.tv_exp);
			viewholder.tv_price = (TextView) v.findViewById(R.id.tv_price);
			viewholder.tv_username = (TextView) v
					.findViewById(R.id.tv_username);

			v.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) v.getTag();
		}
		final OItem o = oitems.get(position);
		if (o != null) {
			if (viewholder.tv_title != null) {
				viewholder.tv_title.setText(o.getTitle());
			}
			if (viewholder.tv_exp != null) {
				viewholder.tv_exp.setText(CommonHelper.timeLeft(Long
						.parseLong(String.valueOf(o.getExp()))));
			}
			if (viewholder.tv_price != null) {
				viewholder.tv_price.setText("$"+String.valueOf(o.getPrice()));
			}
			if (viewholder.tv_username != null) {
				viewholder.tv_username.setText(String.valueOf(o.getUsername()));
			}
		}
		return v;

	}

}
